<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    
   <%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@page import="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Lecturer Detail</title>
	
	<link rel="stylesheet" type="text/css" href="css/stylev2.css" />
	<link rel="stylesheet" type="text/css" href="css/frame2.css" /> 
	
	<style type="text/css">
		a {
	      text-decoration:none;
	   }
	</style>
</head>
<body>

<%
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
			
	// check session
		if (htp_session == null || htp_session.getAttribute("sUser") == null) {
		    // Forward the control to login.jsp if authentication fails or session expires
		    request.getRequestDispatcher("/login.jsp").forward(request,
		        response);
		}
		if(!"admin".equals(htp_session.getAttribute("sUser"))){%>
			<script>alert("This user dont have PERMISSION to access this zone.");</script>
			<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=system_select.jsp"> 	
	<%}
	
	//set Database Connection
	String hostProps = "";
	String usernameProps  = "";
	String passwordProps  = "";
	String databaseProps = "";
	
	try {
		//get current path
		ServletContext servletContext = request.getSession().getServletContext();
		
		InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
		Properties props = new Properties();
		
		props.load(input);

		hostProps  = props.getProperty("host");
		usernameProps  = props.getProperty("username");
		passwordProps  = props.getProperty("password");
		databaseProps = props.getProperty("database");
	} catch (Exception e) { 
		out.println(e);  
	}
	
	// connect database
	Connection connect = null;		
	try {
		Class.forName("com.mysql.jdbc.Driver");
	
		connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/" + databaseProps +
				"?user=" + usernameProps  + "&password=" + passwordProps );
	
		if(connect != null){
			System.out.println("Database Connect Sucesses.");
		} else {
			System.out.println("Database Connect Failed.");	
		}

	} catch (Exception e) {
		out.println(e.getMessage());
		e.printStackTrace();
	}
	
	String index = request.getParameter("index");
	
	System.out.println("lecturer_index  "+index);//////try//////
	
	if(index==null)
	{
		 response.sendRedirect("admin_lecturer.jsp");
	}
	
	System.out.println("@ lecturer_detail index:"+index);
	
	Lecturer lec = new Lecturer();
	
	
	String sql_cmd = " SELECT L.picture ,L.status , N.title_th, N.title_en,"+
			" N.name_th, N.surname_th , N.name_en , N.surname_en ,P.personal_id, P.gender,P.marital_status ,"+
			" P.address , P.phone_number, P.email , P.facebook ,P.date_of_birth ,"+
			" L.management_position_id, E.education_position_name , D.department_name_th"+
			" FROM lecturer L , full_name N , department D ,people P  , education_position E "+
			" WHERE L.index = P.index AND P.full_name_id = N.full_name_id " +
			" AND L.department_id = D.department_id "+
			" AND L.education_position_id = E.education_position_id "+
			" AND L.index = '"+index+"'";
	
	System.out.println("sql_cmd:"+sql_cmd);
	
	ResultSet rs = 
			connect.createStatement().executeQuery(" SELECT L.picture ,L.status , N.title_th,N.title_en, "+
			" N.name_th, N.surname_th , N.name_en , N.surname_en ,P.personal_id, P.gender,P.marital_status ,"+
			" P.address , P.phone_number, P.email , P.facebook ,P.date_of_birth ,"+
			" L.management_position_id, E.education_position_name , D.department_name_th"+
			" FROM lecturer L , full_name N , department D ,people P  , education_position E "+
			" WHERE L.index = P.index AND P.full_name_id = N.full_name_id " +
			" AND L.department_id = D.department_id "+
			" AND L.education_position_id = E.education_position_id "+
			" AND L.index = '"+index+"'");
	
	while(rs.next())
	{
		System.out.println("Found Result");		
		String src_path_full = rs.getString("picture");
		if((src_path_full!=null)&&(!("null").equals(src_path_full)))
		{
			String[] src_path_temp = src_path_full.split("/");	
			int length = src_path_temp.length;
			
			String src_path_a = src_path_temp[length-2];
			String src_path_b = src_path_temp[length-1];
			
			String src_path = src_path_a+"/"+src_path_b;
			
			lec.setPicture(src_path);
		
		}else{
			lec.setPicture("upload/default_person_icon.jpg");
			
		}
		
		
		if(rs.getString("personal_id")==null || "null".equals(rs.getString("personal_id")))
		{
			lec.setPersonalId("");
		}	
		else{
			lec.setPersonalId(rs.getString("personal_id"));
		}
		
		
		
		System.out.println("title_th:"+rs.getString("title_th"));
		if(rs.getString("title_th")==null || "null".equals(rs.getString("title_th")))
		{
			lec.setTitleTH("");	
		}else{
			lec.setTitleTH(rs.getString("title_th"));
		}
		
		if(rs.getString("title_en")==null || "null".equals(rs.getString("title_en")))
		{
			lec.setTitleEN("");	
		}else{
			lec.setTitleEN(rs.getString("title_en"));
		}
		
		
		lec.setNameTH(rs.getString("name_th"));
		lec.setSurnameTH(rs.getString("surname_th"));
		
		
		if(rs.getString("name_en")==null || "null".equals(rs.getString("name_en")))
		{
			lec.setNameEN("");
		}else{
			lec.setNameEN(rs.getString("name_en"));
		}
		
	
		if(rs.getString("surname_en")==null || "null".equals(rs.getString("surname_en")))
		{
			lec.setSurnameEN("");
		}else{
			lec.setSurnameEN(rs.getString("surname_en"));
		}
		
		if(rs.getString("date_of_birth")==null || "null".equals(rs.getString("date_of_birth")))
		{
			lec.setBirtgdate("");
		}else{
			lec.setBirtgdate(rs.getString("date_of_birth"));				
		}
	
					
		if(rs.getString("gender").equals("M"))
		{
			lec.setGender("ชาย");
		}else
		{
			lec.setGender("หญิง");
		}
		
		
		if(rs.getString("marital_status")==null || "null".equals(rs.getString("marital_status")))
		{
			lec.setMaritalStatus("");
			
		}else{
			lec.setMaritalStatus(rs.getString("marital_status"));
		}
			
		
		if(rs.getString("address")==null || "null".equals(rs.getString("address")))
		{
			lec.setAddress("");
		}else{
			lec.setAddress(rs.getString("address"));
		}
		if(rs.getString("phone_number")==null || "null".equals(rs.getString("phone_number")))
		{
			lec.setTelephone("");
		}else{
			lec.setTelephone(rs.getString("phone_number"));
		}
		if(rs.getString("email")==null || "null".equals(rs.getString("email")))
		{
			lec.setEmail("");
		}else{
			lec.setEmail(rs.getString("email"));
		}
		if(rs.getString("status")==null || "null".equals(rs.getString("status")))
		{
			lec.setStatus("");
		}else{
			lec.setStatus(rs.getString("status"));
		}
		if(rs.getString("facebook")==null || "null".equals(rs.getString("facebook")))
		{
			lec.setFacebook("");
		}else{
			lec.setFacebook(rs.getString("facebook"));
		}
		
		lec.setDepartment(rs.getString("department_name_th"));
		
		
		lec.setEducationPosition(rs.getString("education_position_name"));
		
		
		String temp_man_pos = rs.getString("management_position_id");		
		if(("").equals(rs.getString("management_position_id"))){
			
			lec.setManagementPosition("");
			
		}else{
			
			ResultSet rs_man = connect.createStatement().executeQuery(" SELECT management_position_name "+
																	  " FROM management_position "+
																	  " WHERE management_position_id='"+temp_man_pos+"'");
					
			while(rs_man.next()){
				
				lec.setManagementPosition(rs_man.getString("management_position_name"));
				
			}
					
			
		}
		
	}
	
	connect.close();
	
	System.out.println("End Jsp");
	

%>

	<div id="page-wrap">
		<div id="inside">
			<table border="0">
				<tr style="font-size:24px">
					<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
						<div id="header_banner-2"></div>
					</td>
					
					<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
						<p align="right"><label><font size="4" color="#ffffff">username : <%=strUser%>&nbsp;&nbsp;</font></label></p>
						<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
					</td>
				</tr>
			</table>
				
				<div id="main-content-ac">
						
						<div class="imageFrame">อาจารย์ / ข้อมูลส่วนตัว</div>
						
						<br/><br/><br/><br/><br/>
						<center>
							<table width="1000px">								 
									  <tr style="font-size:20px">
										  <td  width="50" height="50" rowspan="3	" style="text-align: center;">
						        			<img id="student_image" src="" style="width:130px;height:160px;	">
						        		</td>
						        		<td height="50" style="font-weight: bold;">เลขบัตรประจำตัวประชาชน</td>
										<td height="50"><%=lec.getPersonalId() %></td>
									  </tr>
									  
									
									  <tr style="font-size:20px">
										<td height="50" style="font-weight: bold;">คำนำหน้า (ภาษาไทย)</td>
										<td height="50"><%=lec.getTitleTH() %></td>           
									  </tr>
									  
									  <tr style="font-size:20px">
										<td height="50" style="font-weight: bold;">ชื่อ (ภาษาไทย)</td>
										<td height="50"><%=lec.getNameTH() %></td>           
									  </tr>
									  
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">นามสกุล (ภาษาไทย)</td>
										<td height="50"><%=lec.getSurnameTH() %></td>           
									  </tr>
									  
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">คำนำหน้า (ภาษาอังกฤษ)</td>
										<td height="50"><%=lec.getTitleEN() %></td>           
									  </tr>
									  
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">ชื่อ (ภาษาอังกฤษ)</td>
										<td height="50"><%=lec.getNameEN() %></td>           
									  </tr>
									  
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">นามสกุล (ภาษาอังกฤษ)</td>
										<td height="50"><%=lec.getSurnameEN() %></td>           
									  </tr>
									  
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">เพศ</td>
										<td height="50"><%=lec.getGender() %></td>           
									  </tr>	
									  
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">สถานะภาพ</td>
										<td height="50"><%=lec.getMaritalStatus() %></td>           
									  </tr>	
									  
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">วัน/เดือน/ปีเกิด</td>
										<td height="50"><%=lec.getBirtgdate() %></td>           
									  </tr>										  
									  
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">ที่อยู่</td>
										<td height="50"><%=lec.getAddress() %></td>           
									  </tr>	
									  
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">เบอร์โทรศัพท์</td>
										<td height="50"><%=lec.getTelephone() %></td>           
									  </tr>
									  
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">อีเมลล์</td>
										<td height="50"><%=lec.getEmail() %></td>           
									  </tr>
									  
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">Facebook</td>
										<td height="50"><%=lec.getFacebook() %></td>           
									  </tr>
									
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">ภาควิชา</td>
										<td height="50"><%=lec.getDepartment() %></td>           
									  </tr>
									  
									   <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">ตำแหน่งทางการศึกษา</td>
										<td height="50"><%=lec.getEducationPosition()%></td>           
									  </tr>
									  
									   <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">ตำแหน่งบริหาร</td>
										<td height="50"><%=lec.getManagementPosition()%></td>           
									  </tr>
									  
									  
									  
									  <tr style="font-size:20px">
									  	<td  width="300" height="50"></td>
										<td height="50" style="font-weight: bold;">สถานะ</td>
										<td height="50"><%=lec.getStatus() %></td>           
									  </tr>
									  
									  <tr style="font-size:20px">
									  	<td width="200" height="90" colspan="3" style="text-align: center;">
										<a href="admin_lecturer.jsp"><button class="buttonStyle">ย้อนกลับ</button></a></center>
										</td>           
									  </tr>
								</table>
						</center>
						
						<br><br><br>
				
			<div style="clear: both;"></div>
				
			<div id="footer"></div>
			
		</div>		
			<div style="clear: both;"></div>		
   </div>

</body>
</html>